package sfmc.common.util;

//base class
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

//user class
import sfmc.common.util.CastUtil;

public class FormatUtil {

	public static String matchFormat(String str, String format) {
		if(str == null || str.length() == 0 ) return "";
		int len = format.length(); 
		char[] result = new char[len];
		for(int i=0,j=0; i<len; i++,j++) {
			if(format.charAt(i)=='#') {
				try {
					result[i]= str.charAt(j);
				}catch(StringIndexOutOfBoundsException e) {
					result[i]= '\u0000';
				}
			} else {
				result[i]= format.charAt(i);
				j--;
			}
		}
		return new String(result);
	}
	
	public static String releaseFormat(String str, String format) {
		if(str == null || str.length() == 0 ) return str;
		int len = format.length();
		char[] result = new char[len];
		for(int i=0,j=0; i<len; i++,j++) {
			if(format.charAt(i)=='#') {
				try {
					result[j] = str.charAt(i);
				}catch(StringIndexOutOfBoundsException e) {
					result[j]= '\u0000';
				}
			} else {
				j--;
			}
		}
		return (new String(result)).trim();
	}

	public static String matchFormat(Date dt, String format) {
		if (dt == null) return "";
		try {
			return new SimpleDateFormat(format).format(dt);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return "invalid format";
 	}
 	
	public static String toCurFormat(String arg) { return toCurFormat(CastUtil.toDbl(arg)); }

	public static String toCurFormat(double arg) { return new DecimalFormat("###,##0").format(arg); }
	
	public static String toPointFormat(String arg, int pointDigit) { return toPointFormat(CastUtil.toDbl(arg), pointDigit); }
	
	public static String toPointFormat(double arg, int pointDigit) {
		if (pointDigit<=0) {
			return new DecimalFormat("##0").format(arg);
		} else {
			String formatStr = "##0.";
			//for (int i=0; i<pointDigit; i++) formatStr += "0";
			formatStr +=pointDigit;
			return new DecimalFormat(formatStr).format(arg);
		}
	}
	
	public static Date toDate(String str) {
		if(str == null || str.length() == 0 ) return null;
		String format = (str.length()==8)? "yyyyMMdd":(str.indexOf("-")>=0)? "yyyy-MM-dd":"yyyy/MM/dd";
		try {
			return new SimpleDateFormat(format).parse(str);
		} catch (ParseException ex) { return null; }
	}

	public static String getDate(String pattern) {

		/*
		  <pattern>
		  G: Era designator
		  y: Year
		  M: Month in year
		  w: Week in year
		  W: Week in month
		  D: Day in year
		  d: Day in month
		  F: Day of week in month
		  E: Day in week
		  a: Am/pm marker
		  H: Hour in day (0-23)
		  k: Hour in day (1-24)
		  K: Hour in am/pm (0-11)
		  h: Hour in am/pm (1-12)
		  m: Minute in hour
		  s: Second in minute
		  S: Millisecond
		  z: Time zone
		  Z: Time zone
		*/

		SimpleDateFormat format = new SimpleDateFormat(pattern);
		Date curDate = new Date();
		return format.format(curDate);
	}

		public static String toPassword(String str, String format) {
			
			if(str == null || str.length() == 0 ) return "";
			if(format.length() != 1) return str;
			
			String result = "";
			for(int i=0; i<str.length(); i++) {
				result += format;
			}
			return result;
		}
}
